home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 7
/
Commodore_Free_Issue_07_2007_Commodore_Computer_Club.d64
/
t.loadstar prg 1
< prev
next >
Wrap
Text File
|
2023-02-26
|
15KB
|
609 lines
uMr LOADSTAR's Intro to Programming
the C64 Part 1
By Dave Moorman
Introduction
The best place in the world to learn
enjoy the art of computer programming
is on the C-64. It is an old machine, &
therefore a lot less complex than new
computers. It has BASIC 2.0 built-in,
plus many Machine Language (ML)
routines that can be used from BASIC.
The processor is the MOS Technology
6510, a member of the 6502 family,
which is very efficient. And the C-64
was designed to be adaptable on many
levels.
But mostly, it is inexpensive. A real
C64 is always available on Ebay. Or,
get the C64DTV & do some hardware
hacking to add a disk drive & key-
board. Or download the Versatile
Commodore Emulator (VICE) from the web
for free. Whichever way you go, you
have a great platform for the kind of
programs one person can sit down &
write. And you can write them just the
way you want.
I am going to make some assumptions.
First, I must assume you have a C-64
and a disk drive or VICE. I assume you
know how to format a disk (on a real
machine) or create & attach a disk
image (with VICE). I even must assume
that if you are using VICE, you know
the different keys you must press for
certain characters.
I also assume that beyond knowing how
to LOAD & RUN a program, you have no
idea how to go about writing one.
That is what we will do in this series
of articles. So, fire up your C64. Get
a new disk ready. Because HERE WE GO!
I DREAM OF GENIE IN A LIGHT BROWN BOX
On the blue screen, you see, in Light
Blue, the word READY. Under it is a
flashing block. This is the cursor. You
probably know what a cursor is, but
just to make sure here is a definition.
The cursor is the place where the next
character you type will appear on the
screen.
Press [Home] & the cursor goes to the
upper left corner of the screen. You
can use the cursor keys to move the
cursor around the screen. On the real
C64, you have two cursor keys -- [Down]
& [Right]. Press [Shift-Down] to go Up,
[Shift-Right] to go Left. (VICE has it
much better -- 4 keys!) I will refer to
Cursor Up, Cursor Down, etc. You will
get used to using the Shift!
Now press [Shift-Home]. This is [CLR],
since it Clears the screen. Cursor
around a bit to get used to it. Now,
crack your knuckles, stretch, & prepare
to meet the Genie!
Inside your computer is a magic genie
who will perform any command you give
it. Try it. Type:
DO MY TAXES
Nothing happened? That is because the
genie doesn't know you are finished
typing. With the cursor flashing on the
same line, press [RETURN].
?SYNTAX ERROR
READY
[]
Syntax Error" means "Huh? I don't
understand." Fortunately, the genie is
always forgiving. See, it is ready
already.
The problem is that the genie under-
stands only a few commands. You must
spell the commands correctly -- because
this is a stupid genie. So try this:
?5+9
And press [RETURN]. (I won't mention it
again. Whenever you are done typing,
press [RETURN].)
14
Pretty clever. What should we call a
magic genie that is really good at
computing math problems? Anyway, the
Question Mark means Print. You could
type out PRINT instead, but why not
just use the single character?
You might have noticed that there is a
space in front of the 1. That is to
leave room for a minus sign, should one
be necessary.
?5-9
-4
We have multiply & divide as well:
?16/2*3
24
The [/] is Divide, [*] is Multiply.
When the genie does math, it always
multiplies & divides first, then adds
and subtracts. For example:
?3+2*5-1
does not equal 20.
12
That is because 2*5 is calculated 1st,
then 3+10-1, which results in 12.
However, you can force one calculation
before another.
?(3+2)*(5-1)
20
There. Calculations in parentheses are
always performed first. So, 3+2=5,
5-1=4, 5*4=20.
So, you have a calculator. Big deal!
But wait. [?] means PRINT. What else
can we print?
?YOUR NAME
0
Ok, I fooled you again. Letters are
used as VARIABLEs -- little boxes
that contain values. The genie thinks
you want to print the contents of a
variable. But you want to print,
literally, "YOUR NAME".
?"YOUR NAME"
YOUR NAME
I hope you used your own name! This
time the genie printed out exactly what
was between the double-quotes. If you
did not use double-quotes [Shift-2] you
probably got a SYNTAX ERROR. But if you
were wrong, don't fret. The genie is
always READY for you to do it right.
The characters between the double-
quotes are said to be in a STRING,
because they are strung together. In
this case, it is a Literal String. What
other kind of string is there?
N$="YOUR NAME"
When you pressed [RETURN] nothing
happened? Do this:
?N$
YOUR NAME
N$ (pronounced "N string") is a String
Variable. It is a box that contains a
string. We also have Numeric
Variables
N=1234
READY.
?N
1234
N & N$ are two different variables.
?N$,N
YOUR NAME 1234
Variables always begin with an alphabet
character (A-Z) & can be 1 or 2 char-
acters long. The 2nd character can be
alpha or a number. A, AB, P0, S5, & RX
are all numeric variables. String
variables have the dollar sign after
the characters. A$, AB$, P0$, S5$, &
RX$ are all string variables. Remember
to pronounce the dollar sign as
"string."
IMMEDIATE vs PROGRAM: The Battle of the
Modes
All this is fine & dandy, but so far we
have nothing much more than a fancy
calculator. That is because we have
been working in IMMEDIATE Mode. That
is, when you press [RETURN], the genie
responds immediately. But we have
another mode. Try this:
10 N$="AMOUNT TENDERED"
Nothing happened, not even the READY.
Actually, a lot happened inside the
machine.
Back in 1976, Bill Gates & Paul Allen
wrote the first BASIC operating system
for the Altair 8800 microcomputer.
Memory was expensive & at a premium. So
a clever idea was developed to indicate
whether what was typed was to be
computed immediately, or put in program
memory.
If the first character(s) of a line are
numeric, the line is considered Program
Mode. The text is placed into Program
Memory, organized by "line numbers." To
look at what is in your program, type
LIST
You will see...
10 N$="AMOUNT TENDERED"
Add two more lines (press [RETURN] for
each):
5 N=35.75
20 ?N$;N
Now list the program again.
5 N=35.75
10 N$="AMOUNT TENDERED"
20 PRINTN$;N
You have just written a program! To
watch it run, type
RUN
You should see:
AMOUNT TENDERED 35.75
This is just a beginning!
SAVING & LOADING YOUR PROGRAMS
Now that you are an honest to goodness
programmer, you will need to save your
program to your disk (presumably in
drive 8 & formatted). The quick way is
to type:
SAVE"MYPROG",8
The filename is "MYPROG" & you are
saving it on drive #8. File names can
be up to 16 characters in length. You
can verify that the program has been
correctly saved with:
VERIFY"MYPROG",8
But this is usually not necessary, if
your drive & disk are in good condition
Now you can turn your computer off,
back on, & load your program:
LOAD"MYPROG",8
then
LIST & RUN
Now for some tricky stuff. If you
change your program, you cannot
simply save it to the same file name.
The file is already on the disk & must
be scratched first. BASIC 2.0 does
have a SAVE@ command, but this has
proven to be buggy, so don't use it!
You can save your changes to a
different filename:
SAVE"MYPROG1",8
But we at LOADSTAR have a better way.
The following code will not make a lot
of sense to you, but that isn't a
problem. Once you type it in exactly as
shown, you will have a SHELL program
you can use for all programs. Type:
NEW
To clear the memory, then type this:
60000 N$="SHELL"
60001 OPEN1,8,15,"S0:"+N$:CLOSE1
60002 SAVEN$,8
Once you have entered the lines
(Pressing [RETURN] after each), type:
GOTO60000
The program, named "SHELL" will be
saved to your disk. Whenever you start
a program, first
LOAD"SHELL",8
LIST it, & change the string in line
60000. One of the neat things about the
C64 is its screen editor. To edit a
line, all you have to do is list it,
LIST60000
Move your cursor up to the place you
want to edit, & type over the text.
Pressing [RETURN] (regardless where it
is on the line) will put the edited
line in memory. So when you start a new
program, make line 60000 read:
60000 N$="NEW NAME"
whatever the new name might be. Then
do the GOTO60000.
A bit of history here. Long ago, I was
working on three interrelated programs
(I will call them PROG1, PROG2, &
PROG3). I had just made some correct-
ions in PROG3, but accidentally saved
it as PROG1. Suddenly, PROG1 was gone.
Hours of programming went where all bad
little files go. I realized I needed a
better way!
My answer was to create a "scratch &
save" routine, with the program name
embedded in the program itself. I chose
line 60000 since BASIC only handles
line number between 0 & 63999. Being
line 60000, the routine is always at
the bottom of the program.
If some mistake or glitch messes up the
code, my line 60000 will be garbled & I
won't be able to save the gobblety-
gook. (There is nothing worse than
accidentally saving corrupted code. You
are very unlikely to revive it. It is
an occasion for a grown man to cry!)
With this routine, every time I save
every program, I use exactly the same
"command:" GOTO60000. My fingers know
this command by heart. Now as I write,
I do a save after entering every few
lines, & especially before I run my
program. I later learned that the gurus
at LOADSTAR had come up with the same
trick, except they used line 10000. The
particular number doesn't matter much
-- just use the same line number all
the time.
Here is what the routine does. First,
you put the program filename in N$.
Then, you use a disk command to scratch
the filename. Lastly, you save the
filename. Again, we will get into the
specifics later in this series. For
now, just use "SHELL" (& change line
60000) to begin all new programs.
READING THE DISK DIRECTORY
Corners had to be cut to put all the
power of BASIC 2.0 in a minimum of
memory. Once such corner is that there
is no Directory command. To see what
is on the disk, you must
1.Save what you are doing (if anything)
2.LOAD"$",8
3.LIST
You will see something like this:
0 ["DISK NAME " 98 2A]
1 "MYPROG" PRG
1 "SHELL" PRG
The top line (in reverse) is the disk
header -- the name you gave the disk
when you formatted it. The number to
the left on the next two lines is the
Block Size of the file. A disk block is
254 bytes in size -- & a 1541 disk has
664 blocks available. Following the
block size is the filename. At the
right is the type of the file. PRG
means Program. The last line tells how
many blocks are available on the disk.
Once you have looked at your directory,
you can reload your program. Clumsy,
yes -- but clever. The directory uses
the same code as a program list, which
saves memory. The only hassle is that
looking at the directory destroys what-
ever you have in program memory at the
time. But of course, you DID save it.
Right?
BACK TO BASIC
It is time to get back to the task at
hand -- learning how to make the
computer do your bidding. Load up
SHELL, list line 60000, & change the
name to "HELLO". You must be as tired
of the all-caps as I am of typing all-
caps. Press [C=-Shift] (that's
[Commodore Logo-Shift]) to switch to
upper/lower case characters. Let's get
to work!
10 ?"[clr]"
20 ?"[down]"
30 ?"Hello, World!"
RUN
There you go! If you list your program,
you will notice that the ?'s have
become PRINT. And PRINT is a very
powerful command in BASIC! When you
press the double-quote, the computer
enters Quote Mode. Anything you type
(other than [RETURN] or ["]) will be
embedded in the string. So, [clr] means
[Shift-Home], & it clears the screen.
[down] is the Cursor Down key.
You can also insert text color changes
right in the string.
30 ?"[ctrl-1]H[ctrl-2]e[ctrl-3]l[ctrl-
4]l[ctrl-5]o, World!"
With this, each character in "Hello"
will have a different color. Here are
the various color controls (which may
be printed on your number keys:
# CTRL C=
1 Black Orange
2 White Brown
3 Red Light Red
4 Cyan Dark Gray
5 Purple Med Gray
6 Green Light Green
7 Blue Light Blue
8 Yellow Light Gray
While we are talking about colors, we
must say we do not have any BASIC
command to change the background or
border color. However, we do have an
all-purpose command that puts infor-
mation right into memory -- POKE. And
the color of the screen background &
border are controlled by two locations
in memory:
53280 - Border Color
53281 - Background Color
So,
25 poke 53281,14
26 poke 53280,0
will change the background to Light
Blue & the border to Black. The numbers
you poke for color are
0 - Black 8 - Orange
1 - White 9 - Brown
2 - Red 10 - Light Red
3 - Cyan 11 - Dark Gray
4 - Purple 12 - Med. Gray
5 - Green 13 - Light Green
6 - Blue 14 - Light Blue
7 - Yellow 15 - Light Gray
As in Immediate Mode, you can print
strings (literal or variables) or
values (constants -- the actual numbers
-- or variables). Numbers are printed
with a preceding space & followed by a
cursor right. Strings are printed
exactly as they appear between the
double-quotes. This is a good time for
you to play around with the PRINT
command. We can print several things
on the same line by using the [;]
(semi-colon) as a separator.
29 n$="Dave"
30 ?"Hello, ";n$
31 age=57
32 ?"You are";age;"years old."
Normally, the PRINT command adds a
"carriage return" at the end of each
line. This means that the cursor moves
down to the next line & to the left
edge of the screen. A semi-colon
"defeats" the carriage return.
So, if you want to print several things
on one line but with different PRINT
commands, put a semi-colon after the
first printed line. Change line 30
above to add a semi-colon after n$, &
see what happens. You will have to
insert a space on line 32 by placing
the cursor over the Y & pressing
[Shift-Ins/Del] -- to add space between
the name & then next sentence. You
can use a comma rather than the semi-
colon, & the cursor will be moved to
the next "tab" column. Try it out &
see how it works. Another way to put
the text where you want it on a line is
with TAB.
40 ?tab(15)"This is nearly centered"
Again, the only way to get acquainted
with the commands & controls is to play
with them. We have a whole slew of
graphic characters available by
pressing the C= key & a letter key. Try
them out. See if you can draw a box.
Another fun exercise is to develop
large letters:
50 ?"[c=-r][space][c=-r][space][c=-r]
51 ?"[c=-q][shift-*][c=-w][space]
[shift-minus]
52 ?"[c=-e][space][c=-e][space][c=-e]
You can create almost any letter using
[C=-Q], [C=-W], [C=-E], [C=-R], [C=-
A], [C=-S], [C=-Z], [C=-X], [Shift-*],
& [Shift-minus] in three layers. The
embedded graphic & control characters
are incredibly difficult to write
about, so I will leave such things up
to you. I am here, after all, to show
you how to program!
And program we will! So, play around.
Have some fun. We will do some serious
computing in the next episode!